
$("#set").click(function(){
	window.location.href="html/set.html";
});
$("#para").click(function(){
	window.location.href="html/para.html";
});
$("#button").click(function(){
	window.location.href="button.html";
});
$("#adj").click(function(){
	window.location.href="adj.html";
});
$("#others").click(function(){
	window.location.href="others.html";
});
$(".relay").click(function(){
	if (window.confirm('发送指令？')){
		$.post("/rst", "relayon",function(ret){

		});
	}else{
		
		return false;
	}
});
$("#wifiset").click(function(){
	var ssid = prompt('名称');
	if (ssid != null && ssid != '') {
		console.log(ssid);
		var pwd = prompt('密码');
		if (pwd != null && pwd != '' && pwd.length >= 8) {
			console.log(pwd);
			$.post("/wificfg", "ssid:"+ssid+";pwd:"+pwd+';',function(ret){
				console.log(ret);
			});
		}
	}
}
);
$("#adjUK").click(function(){
	var val = prompt('输入标准电压值(V)，3位小数') * 1000;
	if (val != null && val != '') {
		$.post("/adjReq", 'KU' + Math.round(val) + ',', function(para){

		});
	}
}
);
$("#adjIK").click(function(){
	var val = prompt('输入标准电流值(A)，3位小数') * 1000;
	if (val != null && val != '') {
		$.post("/adjReq", 'KI' + Math.round(val) + ',', function(para){
	
		});
	}
}
);
$("#pricePerkWh").click(function(){
	var val = prompt('输入电费单价(元/度)，2位小数') * 100;
	if (val != null && val != '') {
		$.post("/adjReq", 'PricePer' + Math.round(val) + ',', function(para){
	
		});
	}
}
);
$("#b_u").click(function(){
	if (window.confirm('保持上建按下，点击确定')){
		$.post("/adjReq", 'BU', function(para){
			
		});
		return true;
	}else{
		
		return false;
	}
});
$("#b_d").click(function(){
	if (window.confirm('保持下建按下，点击确定')){
		$.post("/adjReq", 'BD', function(para){
			
		});
		return true;
	}else{
		
		return false;
	}
});
$("#b_l").click(function(){
	if (window.confirm('保持左建按下，点击确定')){
		$.post("/adjReq", 'BL', function(para){
			
		});
		return true;
	}else{
		
		return false;
	}
});
$("#b_r").click(function(){
	if (window.confirm('保持右建按下，点击确定')){
		$.post("/adjReq", 'BR', function(para){
			
		});
		return true;
	}else{
		
		return false;
	}
});
$("#b_c").click(function(){
	if (window.confirm('保持中建按下，点击确定')){
		$.post("/adjReq", 'BC', function(para){
			
		});
		return true;
	}else{
		
		return false;
	}
});
var upgrading = false;
$("#fota").click(function(){
	if (window.confirm('升级？')){
		$.post("/rst", "ota",function(ret){
			upgrading = true;
		});
		return true;
	}else{
		
		return false;
	}
});
setInterval(function(){
	if (upgrading == true) {
		$.get("/otaInfo", "",function(ret){
			console.log(ret);
			var valArr = ret.split(',');
			if (valArr[1] != '0') {
				if (valArr[1] == valArr[0]) {
					$('#otainfo').text('FOTA完成，请点击重启');
					upgrading = false;
				}
				else {
					$('#otainfo').text('FOTA  [' + valArr[0] + '/' + valArr[1] + '(' + parseInt(valArr[0]/valArr[1]*100) +'%) ]');
				}
			}
			else {
				$('#otainfo').text('FOTA准备中');
			}
		});
	}
}, 100);

$("#rst").click(function(){
	if (window.confirm('重启？')){
		$.post("/rst", "sysRst",function(ret){
			console.log(ret);
		});
		return true;
	}else{
		
		return false;
	}
});

$("#info").click(function(){
	if (window.confirm('CSDN Tyrion.Mon')){
		
		return true;
	}else{
		
		return false;
	}
});

var HlwDataReqCnt = 0;
var HlwDataReqSuccessCnt = 0;
var dataID = ['volt', 'current', 'power', 'energy', 'freq', 'factor', 'energyZero', 'relay', 'powerS', 'hostname', 'uname0', 'uname1', 'uname2', 'uname3', 'uname4', 'money'];
var dataMult = [1000, 1000, 1000, 1000, 100, 100, 1000, 0, 1000, 0, 0, 0, 0, 0, 0, 100];
var cdImgON = false;
var pdImgON = false;
setInterval(function(){
	if (document.getElementById(dataID[0])) {
		HlwDataReqCnt++;
		
		$.get("/hlwData", "",function(ret){
			console.log(ret +' len=' + ret.length);
			var valArr = ret.split(',');
			var float_;
			if (valArr[0] == 'LOG')
			{
				var LogVal = parseInt(valArr[1]);
				var WarnVal = parseInt(valArr[2]);
				$('#warningLog').empty();
				if (LogVal > 0 || WarnVal > 0)
					$('#warningLog').show();
				else
					$('#warningLog').hide();
				if (LogVal & 0x02)
				{
					$('#warningLog').append('<p style="color: darkcyan;">'+'[日志] 倒计时任务结束，关闭输出'+'</p>')
				}
				if (LogVal & 0x04)
				{
					$('#warningLog').append('<p style="color: darkcyan;">'+'[日志] 功率低于一定值任务结束，关闭输出'+'</p>')
				}
				if (WarnVal & 0x01)
				{
					$('#warningLog').append('<p style="color: orange;">'+'[警告] 发生过载事件，关闭输出'+'</p>')
				}
				if (WarnVal & 0x02)
				{
					$('#warningLog').append('<p style="color: orange;">'+'[警告] 继电器输出设定和实际状态不匹配'+'</p>')
				}
			}
			else
			{
				for (var i = 0; i < valArr.length; i++)
				{
					if (i == 7) {
						if (valArr[i] == '0') {
							$('#relayImg').attr('src', 'img/off.png');
						} else {
							$('#relayImg').attr('src', 'img/on.png');
						}
					} else if (i == 9) {
						$("#" + dataID[i]).text(valArr[i]);
					} else if (i >= 10 && i <= 14) {
						if (valArr[10] == '0') {
							$('#cdImg').attr('src', 'img/off.png');
							cdImgON = false;
						} else {
							$('#cdImg').attr('src', 'img/on.png');
							cdImgON = true;
						}
						var int_ = parseInt(valArr[11]);
						$('#cdP').text('    ' + (Math.floor(int_/3600)).toString() + '小时' + (Math.floor(Math.floor(int_/60)%60)).toString() + '分' + (Math.floor(int_%60)).toString() + '秒 后关闭输出');
						if (valArr[12] == '0') {
							$('#pdImg').attr('src', 'img/off.png');
							pdImgON = false;
						} else {
							$('#pdImg').attr('src', 'img/on.png');
							pdImgON = true;
						}
						$('#pdP').text('    有功功率持续低于 ' + valArr[13] + 'W ');
						int_ = parseInt(valArr[14]);
						$('#pdP').append('达 ' + (Math.floor(int_/3600)).toString() + '小时' + (Math.floor(Math.floor(int_/60)%60)).toString() + '分' + (Math.floor(int_%60)).toString() + '秒 后关闭输出');
					} else {
						float_ = parseInt(valArr[i]) / dataMult[i];
						$("#" + dataID[i]).text(float_.toString());
						if (i == 2) {
							var perkWh = '∞';
							if (valArr[i] != '0')
								perkWh = (1000 / float_).toFixed(2);
							$('#hourPerkWh').text(perkWh);
						}
					}
				}
			}
			HlwDataReqSuccessCnt++;
		});
	}
}, 1000);

var paraName = ['电压系数','电流系数','功率系数','视在系数','能量系数','脉冲频率','脉冲计数器','电能计数','可清电能计数','已运行','复位次数','APP版本','电费计数','有功平均值'];
var paraID = ['UC','IAC','PAC','SC','EC','HFC','PFCNT','ECNT','ECNTZERO','sysRun','sysRsted','appVer','moneyCnt','powerPAAver'];
setInterval(function(){
	if (document.getElementById('hlwParaLi')) {	//仅该页面显示时		
		$.get("/hlwAC", "",function(ret){
			console.log(ret +' len=' + ret.length);
			var valArr = ret.split(',');
			
			$("#hlwParaLi").empty();		
			for (var i = 0; i < valArr.length; i++)
			{				
				$("#hlwParaLi").append("<li><p>"+paraName[i]+"</p><span id="+paraID[i]+"></span></li>");	//动态添加列表
				$("#"+paraID[i]).text(valArr[i]);
			}
		});
	}
}, 1000);

var btName = ['上键','下键','左键','右键','中键','当前值'];
var btID = ['b_u_p','b_d_p','b_l_p','b_r_p','b_c_p','b_ad_p'];
setInterval(function(){
	if (document.getElementById(btID[0])) {		
		$.get("/btPara", "",function(ret){
			console.log(ret +' len=' + ret.length);
			var valArr = ret.split(',');
			
			for (var i = 0; i < valArr.length; i++)
			{
				$("#" + btID[i]).text(btName[i] + ': ' + valArr[i]);
			}
		});

	}
}, 1000);

var adjName = ['电压K值','电流K值'];
var adjID = ['adjUK_p','adjIK_p'];
var adjMult = [1000000, 1000000];
setInterval(function(){
	if (document.getElementById(adjID[0])) {
		$.get("/adjPara", "",function(ret){
			console.log(ret);
			var valArr = ret.split(',');
			
			for (var i = 0; i < valArr.length; i++)
			{
				$("#" + adjID[i]).text(adjName[i] + ': ' + (parseInt(valArr[i]) / adjMult[i]));
			}
		});

	}
}, 1000);


setInterval(function(){
	if (document.getElementById('otherSetLi1')) {	//仅该页面显示时		
		$.get("/otherSet", "",function(ret){
			console.log(ret +' len=' + ret.length);
			var valArr = ret.split(',');
			for (var i = 0; i < valArr.length; i++)
			{
				switch (i)
				{
					case 0: if (valArr[i] == '0')
								$('#ImgLast').attr('src', '../img/off.png');
							else
								$('#ImgLast').attr('src', '../img/on.png');
							break;
					case 1: if (valArr[i] == '0')
								$('#ImgDef').attr('src', '../img/off.png');
							else
								$('#ImgDef').attr('src', '../img/on.png');
							break;
					case 2: $('#localNameP').text(valArr[i]); break;
					case 3: $('#severIPP').text(valArr[i]); break;
					case 4: $('#severPORTP').text(valArr[i]); break;
					case 5: var oled = valArr[i].split(' ');
							var mode = '';
							switch (oled[1])
							{
								case '0': mode = '滚动+单行'; break;
								case '1': mode = '滚动+双行'; break;
								case '2': mode = '静止+单行'; break;
								case '3': mode = '静止+双行'; break;
							}
							var off = '';
							switch (oled[2])
							{
								case '0': off = '常亮'; break;
								default: off = '无操作' + oled[2] + '分钟后息屏'; break;
							}
							$('#oledSetP').text('亮度' + oled[0] + ' ' + mode + ' ' + off); 
							break;
					case 6: $('#overloadP').text(valArr[i]); break;
					case 7: $('#pricePerkWhP').text((parseInt(valArr[i])/100).toString() + '元'); break;
				}
			}
		});
	}
}, 1000);

$('#relayLast').click(function(){
	$.post("/rst", "relayLast",function(ret){

	});
});
$('#relayDef').click(function(){
	$.post("/rst", "relayDef",function(ret){

	});
});

$("#localName").click(function(){
	var name = prompt('NAME');
	if (name != null && name != '') {
		$.post("/rst", "localName:" + name + ',', function(ret){

		});
	}
});
$("#severIP").click(function(){
	var name = prompt('IP');
	if (name != null && name != '') {
		$.post("/rst", "serverIP:" + name + ',', function(ret){

		});
	}
});
$("#severPORT").click(function(){
	var name = prompt('端口');
	if (name != null && name != '') {
		$.post("/rst", "serverPORT:" + name + ',', function(ret){

		});
	}
});
$("#zeroable").click(function(){
	if (window.confirm('清零？')){
		$.post("/rst", "zeroable",function(ret){
			console.log(ret);
		});
		return true;
	}else{
		
		return false;
	}
});
$("#priceZeroable").click(function(){
	if (window.confirm('清零电费？')){
		$.post("/rst", "priceZeroable",function(ret){
			console.log(ret);
		});
		return true;
	}else{
		
		return false;
	}
});
$("#oledSet").click(function(){
	var BL = prompt('亮度(0-255)');
	if (BL != null && BL != '') {
		var mode = prompt('模式(0:滚动+单行 1:滚动+双行 2:静止+单行 3:静止+双行) !!!需重启');
		if (mode != null && mode != '') {
			var off = prompt('(0-255) 0:常亮 其它值:N分钟后熄灭');
			if (off != null && off != '') {
				$.post("/rst", "oledSet:" + BL + ',' + mode + ',' + off + ',', function(ret){
				
				});
			}
		}
	}
});
$("#countDown").click(function(){
	if (cdImgON == true) {
		$.post("/rst", 'cdCfg:0,',function(ret){

		});
		return true;
	}
	var hour = prompt('小时');
	if (hour != null && hour != '') {
		console.log(hour);
		var min = prompt('分');
		if (min != null && min != '') {
			console.log(min);
			var sec = prompt('秒');
			if (sec != null && sec != '') {
				console.log(sec);
				var s = hour * 3600 + min * 60 + sec * 1;
				console.log(s);
				$.post("/rst", 'cdCfg:' + s + ',',function(ret){
					console.log(ret);
				});
			}
		}
	}
}
);
$("#powerDown").click(function(){
	if (pdImgON == true) {
		$.post("/rst", 'pdCfg:0,0,',function(ret){
	
		});
		return true;
	}
	var w = prompt('功率临界值');
	if (w != null && w != '') {
		var hour = prompt('小时');
		if (hour != null && hour != '') {
			console.log(hour);
			var min = prompt('分');
			if (min != null && min != '') {
				console.log(min);
				var sec = prompt('秒');
				if (sec != null && sec != '') {
					console.log(sec);
					var s = hour * 3600 + min * 60 + sec * 1;
					console.log(s);
					$.post("/rst", 'pdCfg:' + w + ',' + s + ',',function(ret){
						console.log(ret);
					});
				}
			}
		}
	}
}
);
$("#overload").click(function(){
	var w = prompt('过载功率');
	if (w != null && w != '') {
		var s = prompt('持续秒数');
		if (s != null && s != '') {
			$.post("/rst", 'overload:' + w + ',' + s + ',',function(ret){

			});
		}
	}
}
);

$('#warningLog').click(function(){
	if (window.confirm('清除日志/警报?')){
		$('#warningLog').empty();
		$('#warningLog').hide();
		$.post("/rst", "clearLog",function(ret){
			console.log(ret);
		});
		return true;
	}else{
		
		return false;
	}
});